package Dao;

import UnderAccessDBclass.Druid_utils;
import UnderAccessDBclass.jdbcUtils;
import bean.Dep;

import java.util.ArrayList;

public class DepDao {
    public ArrayList<Dep> selectAll() {
        ArrayList<Dep> depList = new ArrayList<>();
        ArrayList<Object[]> list = Druid_utils.executeQuery("select * from dep", null);
        for (Object[] objects : list) {
            Dep dep = new Dep();
            dep.setDno((String) objects[0]);
            dep.setDname((String) objects[1]);
            dep.setTel((String) objects[2]);
            depList.add(dep);
        }
        return depList;
    }

    public Dep selectByDno(String dno) {
        Dep dep = null;
        ArrayList<Object[]> list = Druid_utils.executeQuery("select * from dep where dno=?", new Object[]{dno});
        if (list.size() > 0) {
            dep = new Dep();
            Object[] objects = list.get(0);
            dep.setDno((String) objects[0]);
            dep.setDname((String) objects[1]);
            dep.setTel((String) objects[2]);
        }

        return dep;
    }

    public Dep selectByDname(String dname) {
        Dep dep = null;
        Object[] params = {dname};
        ArrayList<Object[]> list = Druid_utils.executeQuery("select * from dep where dname=?", params);
        if (list.size() > 0) {
            dep = new Dep();
            Object[] objects = list.get(0);
            dep.setDno((String) objects[0]);
            dep.setDname((String) objects[1]);
            dep.setTel((String) objects[2]);
        }
        return dep;
    }

    public ArrayList<Dep> delete(String dno) {
        String sql = "delete from dep where dno=?";
        Druid_utils.executeUpdate(sql, new Object[]{dno});
        return selectAll();
    }

    public ArrayList<Dep> batchDelete(String[] dno_list) {
        StringBuilder sql_builder = new StringBuilder("DELETE FROM dep WHERE dno IN (");
        Object[] params = new Object[dno_list.length];
        for (int i = 0; i < dno_list.length; i++) {
            params[i] = dno_list[i];
            if (i == dno_list.length - 1) {
                sql_builder.append("?);");
            } else {
                sql_builder.append("?,");
            }
        }
        String sql = sql_builder.toString();
        if (jdbcUtils.executeUpdate(sql, params)) {
            System.out.println("批量删除成功");
        }
        return selectAll();
    }

    public ArrayList<Dep> insert(String sno, String sname, String tel) {
        String sql = "insert into dep values(?,?,?)";
        Druid_utils.executeUpdate(sql, new Object[]{sno, sname, tel});
        return selectAll();
    }

    public ArrayList<Dep> update(String dno, String dname, String tel) {
        String sql = "update dep set dname=?,tel=? where dno=?";
        Druid_utils.executeUpdate(sql, new Object[]{dname, tel, dno});
        return selectAll();
    }

    public static void main(String[] args) {
        DepDao depDao = new DepDao();
        System.out.println(depDao.selectByDname("中文系"));
    }
}
